package com.hiss.basic.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.hiss.basic.client.system.ISysOperateLogService;
import com.hiss.basic.model.system.domain.SysOperateLogDomain;
import com.hiss.basic.model.system.request.SysOperateLogQueryRequest;
import com.hiss.basic.model.system.response.SysOperateLogQueryResponse;
import com.hiss.basic.starter.mybatis.utils.PageUtils;
import com.hiss.basic.system.mapper.SysOperateLogMapper;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 系统操作日志 service impl
 */
@Slf4j
@Service
public class SysOperateLogServiceImpl extends ServiceImpl<SysOperateLogMapper, SysOperateLogDomain> implements ISysOperateLogService {

    @Override
    public Page<SysOperateLogQueryResponse> findPageList(SysOperateLogQueryRequest request) {
        QueryWrapper wrapper = new QueryWrapper();
        if (ObjectUtil.isNotNull(request)) {
            wrapper.like(SysOperateLogDomain::getTitle, request.getTitle(), StrUtil.isNotBlank(request.getTitle()));
            wrapper.eq(SysOperateLogDomain::getBusinessType, request.getBusinessType(), ObjectUtil.isNotNull(request.getBusinessType()));
            wrapper.like(SysOperateLogDomain::getUsername, request.getUsername(), StrUtil.isNotBlank(request.getUsername()));
            wrapper.like(SysOperateLogDomain::getNickname, request.getNickname(), StrUtil.isNotBlank(request.getNickname()));
            wrapper.like(SysOperateLogDomain::getMethod, request.getMethod(), StrUtil.isNotBlank(request.getMethod()));
            wrapper.like(SysOperateLogDomain::getUrl, request.getUrl(), StrUtil.isNotBlank(request.getUrl()));
            wrapper.like(SysOperateLogDomain::getIp, request.getIp(), StrUtil.isNotBlank(request.getIp()));
            wrapper.eq(SysOperateLogDomain::getStatus, request.getStatus(), ObjectUtil.isNotNull(request.getStatus()));
            wrapper.ge(SysOperateLogDomain::getOperateTime, request.getBeginTime(), ObjectUtil.isNotNull(request.getBeginTime()));
            wrapper.le(SysOperateLogDomain::getOperateTime, request.getEndTime(), ObjectUtil.isNotNull(request.getEndTime()));
        }
        wrapper.orderBy(SysOperateLogDomain::getOperateTime, false);

        Page page = PageUtils.getPage();
        return pageAs(page, wrapper, SysOperateLogQueryResponse.class);
    }
}
